package com.example.pingze.service.impl;

import com.example.pingze.entity.UserLogin;
import com.example.pingze.entity.UserLoginDetails;
import com.example.pingze.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private LoginService loginService;
    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        UserLogin user = loginService.findUserLoginByAccount(account);
        if(Objects.isNull(user)) {
            throw new RuntimeException("用户名或密码错误");
        }
        //将权限返回
        return new UserLoginDetails(user);
    }
}
